/**
 * excel导出文件
 */
layui.define(['fsoft', 'table', 'form'], function (exports) {
  "use strict";
  var $ = layui.$
    , form = layui.form
    , fsoft = layui.fsoft
    , table = layui.table
    , zlExcel = {
      downloadexcl: function (params, url) {
        if (!url) {
          url = layui.cache['contentPath'] + '/sms-common/export?tokenId=' + getTokenId();
        }
        var html = ('<form id="downloadexcelform" action="' + url + '" method="post" style="display:none">');
        if ($("#downloadexcelform").length > 0) {
          $("#downloadexcelform").remove();
        }
        if (params) {
          params = JSON.stringify(params).replace(/"/g, "&quot;").replace(/'/g, "&apos;");
          html += '<input name="model" value=\'' + params + '\'/>';
        }
        html += '</form>';
        $("body").append(html);
        $("#downloadexcelform").prop('action', url);
        $("#downloadexcelform").submit();
      }
      /*
      * scope 导出范围
      *   长度!=3：展示全部（含不传）
      *   长度==3：用0/1标识是否显示，1表示显示，如只想显示“当前页”，传入'100'
      * */
      , excelpopform: function (params) {
        var scopeInput = '';
        var scope = params.scope + '';
        if (scope.length == 3) {
          if (scope.substr(0, 1) == '1') {
            scopeInput += '<input type="radio" name="datalist" value="0" title="当前页" checked="">';
          }
          if (scope.substr(1, 1) == '1') {
            if (scopeInput == '') {
              scopeInput += '<input type="radio" name="datalist" value="1" title="选中数据" checked="">';
            } else {
              scopeInput += '<input type="radio" name="datalist" value="1" title="选中数据">';
            }
          }
          if (scope.substr(2, 1) == '1') {
            if (scopeInput == '') {
              scopeInput += '<input type="radio" name="datalist" value="2" title="全部" checked="">';
            } else {
              scopeInput += '<input type="radio" name="datalist" value="2" title="全部">';
            }
          }
        } else {
          scopeInput += '<input type="radio" name="datalist" value="0" title="当前页" checked="">';
          scopeInput += '<input type="radio" name="datalist" value="1" title="选中数据">';
          scopeInput += '<input type="radio" name="datalist" value="2" title="全部">';
        }
        var htmlStr = [
          '<div class="layui-fluid" lay-filter="divgridWorkflow" style="padding:20px 20px 20px 10px">'
          , ' <form id ="excelForm" class="layui-form layui-form-asset"  action="">'
          , '	<div class="layui-form-item">'
          , '		<label class="layui-form-label">标题</label>'
          , '		<div class="layui-input-block">'
          , '			<input type="text" name="filetitle" lay-verify="" placeholder="请输入标题,如无则为系统默认标题,导出CSV设置无效"  class="layui-input">'
          , '		</div>'
          , '	</div>'
          , '	<div class="layui-form-item">'
          , '		<label class="layui-form-label">文件名</label>'
          , '		<div class="layui-input-block">'
          , '			<input class="layui-input" type="text" name="fileName" lay-verify="" placeholder="请输入文件名,如无则为系统默认文件名">'
          , '		</div>'
          , '	</div>'
          , '	<div class="layui-form-item">'
          , '     <label class="layui-form-label">数据设定</label>'
          , '     <div class="layui-input-block">'
          , scopeInput
          , '     </div>'
          , '	</div>'
          , '	<div class="layui-form-item">'
          , '     <label class="layui-form-label">导出文件设定</label>'
          , '     <div class="layui-input-block">'
          , '         <input type="radio" name="filetype" value=".xls" title="Excel文件" checked="">'
          , '         <input type="radio" name="filetype" value=".csv" title="Csv文件" >'
          , '     </div>'
          , '	</div>'
          , '	<div class="layui-hide">'
          , '     <div class="layui-input-block">'
          , '         <button id="confirm-btn" class="layui-btn fr mr10" lay-submit lay-filter="confirm-btn">确定</button>'
          , '     </div>'
          , '	</div>'
          , '</form></div>'
        ]
          , defaultsetting = {
            url: layui.cache['contentPath'] + '/sms-common/export?tokenId=' + getTokenId(),
          }
          , tableid = params.tableid
          , all = params.all == undefined ? true : params.all
          , checklist = tableid ? table.checkStatus(tableid).data : params.checklist
          , columns = params.columns ? params.columns : ''
          , filetitle = params.filetitle ? params.filetitle : ''
          , fileName = params.fileName ? params.fileName : ''
          , curpagelist = tableid ? layui.table.cache[tableid] : params.curpagelist;
        params = $.extend({}, defaultsetting, params);
        if (tableid && !columns) {
          columns = {};
          table.eachCols(tableid, function (i3, item3) {
            if (item3.field && item3.type == 'normal' && !item3.hide) {
              columns[item3.title] = item3.field;
            }
          });
        }
        if (params.url) {
          if (params.url.indexOf("?tokenId") == -1) {
            params.url = params.url + "?tokenId=" + getTokenId();// 解决多用户导出报错问题
          }
        }
        if (!columns) {
          fsoft.errorMsg("缺少参数配置，请检查参数配置！");
          return;
        }
        layer.open({
          type: 1,
          title: '导出数据选择',
          id: 'excelpopLayer',
          area: ['600px', '280px'],
          btn: ['确定', '取消'],
          content: htmlStr.join(''),
          success: function (layero, index) {
            if (filetitle) {
              $('input[name="filetitle"]').val(filetitle);
            }
            if (fileName) {
              $('input[name="fileName"]').val(fileName);
            }
            form.render();
            if (!all) {
              $('#excelForm').find('input[value="2"]').next(".layui-form-radio").hide();
            }
            form.on('submit(confirm-btn)', function (data) {
              var newparam = data.field;
              if (newparam.datalist == "0") {
                newparam.list = curpagelist;
              } else if (newparam.datalist == "1") {
                newparam.list = checklist;
                if (!checklist || checklist.length == 0) {
                  fsoft.warnMsg("无数据选中,请先选择数据！");
                  return false;
                }
              }
              newparam.columns = columns;
              newparam.MAINTABLENAME_ZLFW = params.MAINTABLENAME_ZLFW;
              newparam = $.extend({}, params, newparam);
              if (newparam.datalist == "2" && (!newparam.sql)) {
                table.exportFileZl(tableid, newparam);
              } else {
                zlExcel.downloadexcl(newparam, params.url);
              }
              layer.close(index);
              return false;
            });
          },
          yes: function (layero, index) {
            document.getElementById("confirm-btn").click();
          }
        });
      }
    }
  exports("zlExcel", zlExcel);
});
